package com.grape.ciq.api.redbook.base;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import com.grape.ciq.api.redbook.internal.parse.MyX509TrustManager;

public class MyHttps {

    /**
     * 发起Https的get请求
     *
     * @param property 请求头参数
     * @url 请求的地址 * @return 响应内容[Map<String, List<String>>(响应头),String(响应体)]
	 *
     */
    public static String get(String url, String outputData, String requestMethod, String sign, String appKey) {
        Map<String, List<String>> headerFields = null;
        StringBuffer buffer = null;
        String response = "";
        try {
            // 创建SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLS");
            TrustManager[] tm = {new MyX509TrustManager()};
            // 初始化
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 获取SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            URL u = new URL(url);
            HttpsURLConnection conn = (HttpsURLConnection) u.openConnection();
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("app-key", appKey);
            conn.setRequestProperty("sign", sign);
            conn.setRequestProperty("timestamp", System.currentTimeMillis() / 1000 + "");
            // 设置当前实例使用的SSLSoctetFactory
            System.out.println("开始链接");
            conn.setSSLSocketFactory(ssf);
            conn.connect();
            System.out.println("链接成功");
            // 读取服务器返回的响应头
            headerFields = conn.getHeaderFields();
            // 读取服务器端返回的内容
            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        response = buffer.toString();
        return response;
    }

    /**
     * https的post请求
     *
     * @param url 目标地址
     * @param property 请求头参数
     * @param outputData 请求内容
     * @return 响应内容[Map<String, List<String>>(响应头),String(响应体)]
     */
    public static String post(String url, String outputData, String requestMethod, String sign, String appKey) {
        StringBuffer buffer = null;
        Map<String, List<String>> headerFields = null;
        String response = "";
        try {
            // 创建SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLS");
            TrustManager[] tm = {new MyX509TrustManager()};
            // 初始化
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 获取SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            URL u = new URL(url);
            HttpsURLConnection conn = (HttpsURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("app-key", appKey);
            conn.setRequestProperty("sign", sign);
            conn.setRequestProperty("timestamp", System.currentTimeMillis() / 1000 + "");
            // 设置当前实例使用的SSLSoctetFactory
            System.out.println("开始链接");
            conn.setSSLSocketFactory(ssf);
            conn.connect();
            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            os.writeBytes(outputData);
            os.flush();
            os.close();
            System.out.println("链接成功");
            // 读取服务器返回的响应头
            headerFields = conn.getHeaderFields();
            // 读取服务器端返回的内容
            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response = buffer.toString();
        return response;
    }

    /**
     * https的put请求
     *
     * @param url 目标地址
     * @param property 请求头参数
     * @param outputData 请求内容
     * @return 响应内容[Map<String, List<String>>(响应头),String(响应体)]
     */
    public static String put(String url, String outputData, String requestMethod, String sign, String appKey) {
        StringBuffer buffer = null;
        Map<String, List<String>> headerFields = null;
        String response = "";
        try {
            // 创建SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLS");
            TrustManager[] tm = {new MyX509TrustManager()};
            // 初始化
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 获取SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            URL u = new URL(url);
            HttpsURLConnection conn = (HttpsURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("PUT");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("app-key", appKey);
            conn.setRequestProperty("sign", sign);
            conn.setRequestProperty("timestamp", System.currentTimeMillis() / 1000 + "");
            // 设置当前实例使用的SSLSoctetFactory
            System.out.println("开始链接");
            conn.setSSLSocketFactory(ssf);
            conn.connect();
            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            os.writeBytes(outputData);
            os.flush();
            os.close();
            System.out.println("链接成功");
            // 读取服务器返回的响应头
            headerFields = conn.getHeaderFields();
            // 读取服务器端返回的内容
            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response = buffer.toString();
        return response;
    }
}
